/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Global;

import Army.Arrow;
import Army.building.BuildingBarrack;
import Army.building.BuildingCastle;
import Army.building.BuildingHouse;
import Army.building.BuildingMine;
import Army.building.BuildingTower;
import Army.Stab;
import Army.unit.UnitArcher;
import Army.unit.UnitSpearMan;
import Army.unit.UnitSwordMan;
import Army.unit.UnitWorker;
import Main.MusicFactory;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

/**
 *
 * @author Huy
 */
public class ResourceManager {

	public static final float[][] teamColors =
			new float[][]{
		{1, 0.5f, 0.5f},
		{1, 1, 0},
		{0.5f, 1, 0.5f},
		{0, 1, 1},
		{0.5f, 0.5f, 1},
		{1, 0, 1}
	};
	final static String texturesFileName = "Textures.xml";
	final static String armyFileName = "Army.xml";
	private static Document resXML;
	private static XPath xpath;
	private static BufferedImage errorImage;
	private static String rootPath;

	public static void initResource(String path) throws Exception {
		rootPath = path + "/";
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		resXML = builder.parse(rootPath + texturesFileName);
		XPathFactory xPathfactory = XPathFactory.newInstance();
		xpath = xPathfactory.newXPath();
		
	}

	public static void InitArmy() throws Exception {
		InitUnits();
		InitBuildings();
	}

	private static String getXPathText(Document xmlDoc, String path) throws XPathExpressionException {
		XPathExpression expr = xpath.compile(path);
		Node node = (Node) expr.evaluate(xmlDoc, XPathConstants.NODE);
		String str = node.getTextContent();
		return str;
	}
	public static BufferedImage getAudio(String path) {

		try {
			XPathExpression expr = xpath.compile("/root/" + path);
			Node node = (Node) expr.evaluate(resXML, XPathConstants.NODE);
			String str = rootPath + node.getTextContent();
			return ImageIO.read(new File(str));
		} catch (XPathExpressionException | DOMException | IOException ex) {
			if (errorImage == null) {
				initErrorImage();
			}
			Logger.getLogger(ResourceManager.class.getName()).log(Level.SEVERE, null, ex);
			return errorImage;
		}
	}
	public static BufferedImage getImage(String path) {

		try {
			XPathExpression expr = xpath.compile("/root/" + path);
			Node node = (Node) expr.evaluate(resXML, XPathConstants.NODE);
			String str = rootPath + node.getTextContent();
			return ImageIO.read(new File(str));
		} catch (XPathExpressionException | DOMException | IOException ex) {
			if (errorImage == null) {
				initErrorImage();
			}
			Logger.getLogger(ResourceManager.class.getName()).log(Level.SEVERE, null, ex);
			return errorImage;
		}
	}

	public static BufferedImage readImageFile(String str) {

		try {
			return ImageIO.read(new File(str));
		} catch (Exception ex) {
			if (errorImage == null) {
				initErrorImage();
			}
			Logger.getLogger(ResourceManager.class.getName()).log(Level.SEVERE, null, ex);
			return errorImage;
		}
	}


	public static void initErrorImage() {
		errorImage = new BufferedImage(40, 40, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g = (Graphics2D) errorImage.getGraphics();
		g.setColor(Color.RED);
		g.fillRect(0, 0, 20, 20);
		g.setColor(Color.GREEN);
		g.fillRect(20, 20, 40, 40);
	}

	public static BufferedImage filtColor(BufferedImage img, float r, float g, float b) {
		BufferedImage newImg = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
		for (int i = 0; i < img.getHeight(); i++) {
			for (int j = 0; j < img.getWidth(); j++) {
				int color = img.getRGB(j, i);
				int red = (color & 0x00ff0000) >> 16;
				int green = (color & 0x0000ff00) >> 8;
				int blue = color & 0x000000ff;
				if (red == green && red == blue) {
					red *= r;
					green *= g;
					blue *= b;
				}
				int newColor = (color & 0xff000000) | (red << 16) | (green << 8) | blue;

				newImg.setRGB(j, i, newColor);

			}
		}
		return newImg;
	}

	protected static void InitUnits() throws Exception {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();

		Document armyXML = builder.parse(rootPath + armyFileName);

		int n = teamColors.length;

		BufferedImage imgSwordMan = readImageFile(rootPath + getXPathText(armyXML, "/root/swordman/sprite"));
		BufferedImage imgArcher = readImageFile(rootPath + getXPathText(armyXML, "/root/archer/sprite"));
		BufferedImage imgSpearMan = readImageFile(rootPath + getXPathText(armyXML, "/root/spearman/sprite"));
		BufferedImage imgWorker = readImageFile(rootPath + getXPathText(armyXML, "/root/worker/sprite"));
		UnitWorker.SPRITES = new BufferedImage[n];
		UnitSwordMan.SPRITES = new BufferedImage[n];
		UnitSpearMan.SPRITES = new BufferedImage[n];
		UnitArcher.SPRITES = new BufferedImage[n];
		for (int i = 0; i < n; i++) {
			UnitWorker.SPRITES[i] = ResourceManager.filtColor(imgWorker
					, teamColors[i][0], teamColors[i][1], teamColors[i][2]);
			UnitSwordMan.SPRITES[i] = ResourceManager.filtColor(imgSwordMan
					, teamColors[i][0], teamColors[i][1], teamColors[i][2]);
			UnitSpearMan.SPRITES[i] = ResourceManager.filtColor(imgSpearMan
					, teamColors[i][0], teamColors[i][1], teamColors[i][2]);
			UnitArcher.SPRITES[i] = ResourceManager.filtColor(imgArcher
					, teamColors[i][0], teamColors[i][1], teamColors[i][2]);
		}
		UnitArcher.SPRITE_ROW = Integer.parseInt(getXPathText(armyXML, "root/archer/nrow"));
		UnitSwordMan.SPRITE_ROW = Integer.parseInt(getXPathText(armyXML, "root/swordman/nrow"));
		UnitSpearMan.SPRITE_ROW = Integer.parseInt(getXPathText(armyXML, "root/spearman/nrow"));
		UnitWorker.SPRITE_ROW = Integer.parseInt(getXPathText(armyXML, "root/worker/nrow"));

		UnitArcher.SPRITE_COL = Integer.parseInt(getXPathText(armyXML, "root/archer/ncolumn"));
		UnitSwordMan.SPRITE_COL = Integer.parseInt(getXPathText(armyXML, "root/swordman/ncolumn"));
		UnitSpearMan.SPRITE_COL = Integer.parseInt(getXPathText(armyXML, "root/spearman/ncolumn"));
		UnitWorker.SPRITE_COL = Integer.parseInt(getXPathText(armyXML, "root/worker/ncolumn"));

		UnitArcher.SPRITE_MID_POINT = text2Point(getXPathText(armyXML, "root/archer/midpoint"));
		UnitSwordMan.SPRITE_MID_POINT = text2Point(getXPathText(armyXML, "root/swordman/midpoint"));
		UnitSpearMan.SPRITE_MID_POINT = text2Point(getXPathText(armyXML, "root/spearman/midpoint"));
		UnitWorker.SPRITE_MID_POINT = text2Point(getXPathText(armyXML, "root/worker/midpoint"));

		UnitArcher.MOVE_SEQUENCE = text2Array(getXPathText(armyXML, "root/archer/movesequence"));
		UnitSwordMan.MOVE_SEQUENCE = text2Array(getXPathText(armyXML, "root/swordman/movesequence"));
		UnitSpearMan.MOVE_SEQUENCE = text2Array(getXPathText(armyXML, "root/spearman/movesequence"));
		UnitWorker.MOVE_SEQUENCE = text2Array(getXPathText(armyXML, "root/worker/movesequence"));

		UnitArcher.IDLE_SEQUENCE = text2Array(getXPathText(armyXML, "root/archer/idlesequence"));
		UnitSwordMan.IDLE_SEQUENCE = text2Array(getXPathText(armyXML, "root/swordman/idlesequence"));
		UnitSpearMan.IDLE_SEQUENCE = text2Array(getXPathText(armyXML, "root/spearman/idlesequence"));
		UnitWorker.IDLE_SEQUENCE = text2Array(getXPathText(armyXML, "root/worker/idlesequence"));

		UnitArcher.ATTACK_SEQUENCE = text2Array(getXPathText(armyXML, "root/archer/attacksequence"));
		UnitSwordMan.ATTACK_SEQUENCE = text2Array(getXPathText(armyXML, "root/swordman/attacksequence"));
		UnitSpearMan.ATTACK_SEQUENCE = text2Array(getXPathText(armyXML, "root/spearman/attacksequence"));
		UnitWorker.ATTACK_SEQUENCE = text2Array(getXPathText(armyXML, "root/worker/attacksequence"));

		UnitArcher.DIE_SEQUENCE = text2Array(getXPathText(armyXML, "root/archer/diesequence"));
		UnitSwordMan.DIE_SEQUENCE = text2Array(getXPathText(armyXML, "root/swordman/diesequence"));
		UnitSpearMan.DIE_SEQUENCE = text2Array(getXPathText(armyXML, "root/spearman/diesequence"));
		UnitWorker.DIE_SEQUENCE = text2Array(getXPathText(armyXML, "root/worker/diesequence"));

		UnitArcher.ATTACK_RANGE = Integer.parseInt(getXPathText(armyXML, "root/archer/attackrange"));
		UnitSwordMan.ATTACK_RANGE = Integer.parseInt(getXPathText(armyXML, "root/swordman/attackrange"));
		UnitSpearMan.ATTACK_RANGE = Integer.parseInt(getXPathText(armyXML, "root/spearman/attackrange"));
		UnitWorker.ATTACK_RANGE = Integer.parseInt(getXPathText(armyXML, "root/worker/attackrange"));

		UnitArcher.VIEW_RANGE = Integer.parseInt(getXPathText(armyXML, "root/archer/viewrange"));
		UnitSwordMan.VIEW_RANGE = Integer.parseInt(getXPathText(armyXML, "root/swordman/viewrange"));
		UnitSpearMan.VIEW_RANGE = Integer.parseInt(getXPathText(armyXML, "root/spearman/viewrange"));
		UnitWorker.VIEW_RANGE = Integer.parseInt(getXPathText(armyXML, "root/worker/viewrange"));

		UnitArcher.HEALTH_POINT = Integer.parseInt(getXPathText(armyXML, "root/archer/healthpoint"));
		UnitSwordMan.HEALTH_POINT = Integer.parseInt(getXPathText(armyXML, "root/swordman/healthpoint"));
		UnitSpearMan.HEALTH_POINT = Integer.parseInt(getXPathText(armyXML, "root/spearman/healthpoint"));
		UnitWorker.HEALTH_POINT = Integer.parseInt(getXPathText(armyXML, "root/worker/healthpoint"));

		UnitArcher.DAMAGE = Integer.parseInt(getXPathText(armyXML, "root/archer/damage"));
		UnitSwordMan.DAMAGE = Integer.parseInt(getXPathText(armyXML, "root/swordman/damage"));
		UnitSpearMan.DAMAGE = Integer.parseInt(getXPathText(armyXML, "root/spearman/damage"));
		UnitWorker.DAMAGE = Integer.parseInt(getXPathText(armyXML, "root/worker/damage"));

		UnitArcher.DEFEND = Integer.parseInt(getXPathText(armyXML, "root/archer/defend"));
		UnitSwordMan.DEFEND = Integer.parseInt(getXPathText(armyXML, "root/swordman/defend"));
		UnitSpearMan.DEFEND = Integer.parseInt(getXPathText(armyXML, "root/spearman/defend"));
		UnitWorker.DEFEND = Integer.parseInt(getXPathText(armyXML, "root/worker/defend"));

		UnitArcher.ATTACK_SPEED = Integer.parseInt(getXPathText(armyXML, "root/archer/attackspeed"));
		UnitSwordMan.ATTACK_SPEED = Integer.parseInt(getXPathText(armyXML, "root/swordman/attackspeed"));
		UnitSpearMan.ATTACK_SPEED = Integer.parseInt(getXPathText(armyXML, "root/spearman/attackspeed"));
		UnitWorker.ATTACK_SPEED = Integer.parseInt(getXPathText(armyXML, "root/worker/attackspeed"));

		UnitArcher.MOVE_SPEED = Double.parseDouble(getXPathText(armyXML, "root/archer/movespeed"));
		UnitSwordMan.MOVE_SPEED = Double.parseDouble(getXPathText(armyXML, "root/swordman/movespeed"));
		UnitSpearMan.MOVE_SPEED = Double.parseDouble(getXPathText(armyXML, "root/spearman/movespeed"));
		UnitWorker.MOVE_SPEED = Double.parseDouble(getXPathText(armyXML, "root/worker/movespeed"));
		
		UnitArcher.BUILD_TIME = Integer.parseInt(getXPathText(armyXML, "root/archer/buildtime"));
		UnitSwordMan.BUILD_TIME = Integer.parseInt(getXPathText(armyXML, "root/swordman/buildtime"));
		UnitSpearMan.BUILD_TIME = Integer.parseInt(getXPathText(armyXML, "root/spearman/buildtime"));
		UnitWorker.BUILD_TIME = Integer.parseInt(getXPathText(armyXML, "root/worker/buildtime"));
		
		UnitArcher.BUILD_COST = Integer.parseInt(getXPathText(armyXML, "root/archer/buildcost"));
		UnitSwordMan.BUILD_COST = Integer.parseInt(getXPathText(armyXML, "root/swordman/buildcost"));
		UnitSpearMan.BUILD_COST = Integer.parseInt(getXPathText(armyXML, "root/spearman/buildcost"));
		UnitWorker.BUILD_COST = Integer.parseInt(getXPathText(armyXML, "root/worker/buildcost"));
		
		UnitArcher.FOOD = Integer.parseInt(getXPathText(armyXML, "root/archer/food"));
		UnitSwordMan.FOOD = Integer.parseInt(getXPathText(armyXML, "root/swordman/food"));
		UnitSpearMan.FOOD = Integer.parseInt(getXPathText(armyXML, "root/spearman/food"));
		UnitWorker.FOOD = Integer.parseInt(getXPathText(armyXML, "root/worker/food"));
		/*
		Arrow.N_ROW = Integer.parseInt(getXPathText(armyXML, "root/arrow/nrow"));
		Arrow.N_COL = Integer.parseInt(getXPathText(armyXML, "root/arrow/ncolumn"));*/
	}

	protected static void InitBuildings() throws Exception {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();

		Document armyXML = builder.parse(rootPath + armyFileName);

		int n = teamColors.length;

		BufferedImage imgTower = readImageFile(rootPath + getXPathText(armyXML, "/root/tower/sprite"));
		BufferedImage imgCastle = readImageFile(rootPath + getXPathText(armyXML, "/root/castle/sprite"));
		BufferedImage imgHouse = readImageFile(rootPath + getXPathText(armyXML, "/root/house/sprite"));
		BufferedImage imgMine = readImageFile(rootPath + getXPathText(armyXML, "/root/mine/sprite"));
		BufferedImage imgBarrack = readImageFile(rootPath + getXPathText(armyXML, "/root/barrack/sprite"));
		BuildingTower.SPRITES = new BufferedImage[n];
		BuildingCastle.SPRITES = new BufferedImage[n];
		BuildingHouse.SPRITES = new BufferedImage[n];
		BuildingMine.SPRITES = new BufferedImage[n];
		BuildingBarrack.SPRITES = new BufferedImage[n];
		for (int i = 0; i < n; i++) {
			BuildingTower.SPRITES[i] = ResourceManager.filtColor(imgTower
					, teamColors[i][0], teamColors[i][1], teamColors[i][2]);
			BuildingCastle.SPRITES[i] = ResourceManager.filtColor(imgCastle
					, teamColors[i][0], teamColors[i][1], teamColors[i][2]);
			BuildingHouse.SPRITES[i] = ResourceManager.filtColor(imgHouse
					, teamColors[i][0], teamColors[i][1], teamColors[i][2]);
			BuildingMine.SPRITES[i] = ResourceManager.filtColor(imgMine
					, teamColors[i][0], teamColors[i][1], teamColors[i][2]);
			BuildingBarrack.SPRITES[i] = ResourceManager.filtColor(imgBarrack
					, teamColors[i][0], teamColors[i][1], teamColors[i][2]);
		}

		BuildingTower.SPRITE_ROW = Integer.parseInt(getXPathText(armyXML, "root/tower/nrow"));
		BuildingCastle.SPRITE_ROW = Integer.parseInt(getXPathText(armyXML, "root/castle/nrow"));
		BuildingHouse.SPRITE_ROW = Integer.parseInt(getXPathText(armyXML, "root/house/nrow"));
		BuildingMine.SPRITE_ROW = Integer.parseInt(getXPathText(armyXML, "root/mine/nrow"));
		BuildingBarrack.SPRITE_ROW = Integer.parseInt(getXPathText(armyXML, "root/barrack/nrow"));

		BuildingTower.SPRITE_COL = Integer.parseInt(getXPathText(armyXML, "root/tower/ncolumn"));
		BuildingCastle.SPRITE_COL = Integer.parseInt(getXPathText(armyXML, "root/castle/ncolumn"));
		BuildingHouse.SPRITE_COL = Integer.parseInt(getXPathText(armyXML, "root/house/ncolumn"));
		BuildingMine.SPRITE_COL = Integer.parseInt(getXPathText(armyXML, "root/mine/ncolumn"));
		BuildingBarrack.SPRITE_COL = Integer.parseInt(getXPathText(armyXML, "root/barrack/ncolumn"));

		BuildingTower.SPRITE_MID_POINT = text2Point(getXPathText(armyXML, "root/tower/midpoint"));
		BuildingCastle.SPRITE_MID_POINT = text2Point(getXPathText(armyXML, "root/castle/midpoint"));
		BuildingHouse.SPRITE_MID_POINT = text2Point(getXPathText(armyXML, "root/house/midpoint"));
		BuildingMine.SPRITE_MID_POINT = text2Point(getXPathText(armyXML, "root/mine/midpoint"));
		BuildingBarrack.SPRITE_MID_POINT = text2Point(getXPathText(armyXML, "root/barrack/midpoint"));
		
		BuildingTower.ATTACK_RANGE = Integer.parseInt(getXPathText(armyXML, "root/tower/attackrange"));
		BuildingCastle.ATTACK_RANGE = Integer.parseInt(getXPathText(armyXML, "root/castle/attackrange"));
		BuildingHouse.ATTACK_RANGE = Integer.parseInt(getXPathText(armyXML, "root/house/attackrange"));
		BuildingMine.ATTACK_RANGE = Integer.parseInt(getXPathText(armyXML, "root/mine/attackrange"));
		BuildingBarrack.ATTACK_RANGE = Integer.parseInt(getXPathText(armyXML, "root/barrack/attackrange"));
		
		BuildingTower.VIEW_RANGE = Integer.parseInt(getXPathText(armyXML, "root/tower/viewrange"));
		BuildingCastle.VIEW_RANGE = Integer.parseInt(getXPathText(armyXML, "root/castle/viewrange"));
		BuildingHouse.VIEW_RANGE = Integer.parseInt(getXPathText(armyXML, "root/house/viewrange"));
		BuildingMine.VIEW_RANGE = Integer.parseInt(getXPathText(armyXML, "root/mine/viewrange"));
		BuildingBarrack.VIEW_RANGE = Integer.parseInt(getXPathText(armyXML, "root/barrack/viewrange"));

		BuildingTower.ATTACK_SPEED = Integer.parseInt(getXPathText(armyXML, "root/tower/attackspeed"));
		BuildingCastle.ATTACK_SPEED = Integer.parseInt(getXPathText(armyXML, "root/castle/attackspeed"));
		BuildingHouse.ATTACK_SPEED = Integer.parseInt(getXPathText(armyXML, "root/house/attackspeed"));
		BuildingMine.ATTACK_SPEED = Integer.parseInt(getXPathText(armyXML, "root/mine/attackspeed"));
		BuildingBarrack.ATTACK_SPEED = Integer.parseInt(getXPathText(armyXML, "root/barrack/attackspeed"));
		
		BuildingTower.DAMAGE = Integer.parseInt(getXPathText(armyXML, "root/tower/damage"));
		BuildingCastle.DAMAGE = Integer.parseInt(getXPathText(armyXML, "root/castle/damage"));
		BuildingHouse.DAMAGE = Integer.parseInt(getXPathText(armyXML, "root/house/damage"));
		BuildingMine.DAMAGE = Integer.parseInt(getXPathText(armyXML, "root/mine/damage"));
		BuildingBarrack.DAMAGE = Integer.parseInt(getXPathText(armyXML, "root/barrack/damage"));
		
		BuildingTower.DEFEND = Integer.parseInt(getXPathText(armyXML, "root/tower/defend"));
		BuildingCastle.DEFEND = Integer.parseInt(getXPathText(armyXML, "root/castle/defend"));
		BuildingHouse.DEFEND = Integer.parseInt(getXPathText(armyXML, "root/house/defend"));
		BuildingMine.DEFEND = Integer.parseInt(getXPathText(armyXML, "root/mine/defend"));
		BuildingBarrack.ATTACK_SPEED = Integer.parseInt(getXPathText(armyXML, "root/barrack/defend"));
		
		BuildingTower.HEALTH_POINT = Integer.parseInt(getXPathText(armyXML, "root/tower/healthpoint"));
		BuildingCastle.HEALTH_POINT = Integer.parseInt(getXPathText(armyXML, "root/castle/healthpoint"));
		BuildingHouse.HEALTH_POINT = Integer.parseInt(getXPathText(armyXML, "root/house/healthpoint"));
		BuildingMine.HEALTH_POINT = Integer.parseInt(getXPathText(armyXML, "root/mine/healthpoint"));
		BuildingBarrack.HEALTH_POINT = Integer.parseInt(getXPathText(armyXML, "root/barrack/healthpoint"));
		
		BuildingTower.BUILD_TIME = Integer.parseInt(getXPathText(armyXML, "root/tower/buildtime"));
		BuildingCastle.BUILD_TIME = Integer.parseInt(getXPathText(armyXML, "root/castle/buildtime"));
		BuildingHouse.BUILD_TIME = Integer.parseInt(getXPathText(armyXML, "root/house/buildtime"));
		BuildingMine.BUILD_TIME = Integer.parseInt(getXPathText(armyXML, "root/mine/buildtime"));
		BuildingBarrack.BUILD_TIME = Integer.parseInt(getXPathText(armyXML, "root/barrack/buildtime"));
		
		BuildingTower.BUILD_COST = Integer.parseInt(getXPathText(armyXML, "root/tower/buildcost"));
		BuildingCastle.BUILD_COST = Integer.parseInt(getXPathText(armyXML, "root/castle/buildcost"));
		BuildingHouse.BUILD_COST = Integer.parseInt(getXPathText(armyXML, "root/house/buildcost"));
		BuildingMine.BUILD_COST = Integer.parseInt(getXPathText(armyXML, "root/mine/buildcost"));
		BuildingBarrack.BUILD_COST = Integer.parseInt(getXPathText(armyXML, "root/barrack/buildcost"));
		
		BuildingTower.FOOD = Integer.parseInt(getXPathText(armyXML, "root/tower/food"));
		BuildingCastle.FOOD = Integer.parseInt(getXPathText(armyXML, "root/castle/food"));
		BuildingHouse.FOOD = Integer.parseInt(getXPathText(armyXML, "root/house/food"));
		BuildingMine.FOOD = Integer.parseInt(getXPathText(armyXML, "root/mine/food"));
		BuildingBarrack.FOOD = Integer.parseInt(getXPathText(armyXML, "root/barrack/food"));
		
		Arrow.SPRITE = ResourceManager.getImage("ingame/arrow");
		Stab.SPRITE = ResourceManager.getImage("ingame/stab");
	}

	public static Point text2Point(String str) {
		String[] strs = str.split(" ");
		int x = Integer.parseInt(strs[0]);
		int y = Integer.parseInt(strs[1]);
		return new Point(x, y);
	}

	public static int[] text2Array(String str) {
		String[] strs = str.split(" ");
		int[] a = new int[strs.length];
		for (int i = 0; i < a.length; i++) {
			a[i] = Integer.parseInt(strs[i]);
		}

		return a;
	}
}
